
import shutil, os
from pathlib import Path
import datetime
import  MyUtils


# # -*- coding: utf-8 -*-
# import cv2
# import numpy as np
# from PIL import ImageFont, Image, ImageDraw

# dirNmae = '\\templates\\mask-jinshouzhi\\'
def copyModelFiles(orgPath, dstPath, dirName, modelIdx, copyImg):
    fromPath = orgPath+ dirName
    toPath = dstPath + dirName
    if os.path.exists(fromPath):
        MyUtils.deleteDir(toPath)
        MyUtils.createDirs(toPath)
        files, wholeFiles = MyUtils.recursive_listdir(fromPath)
        for file in files:
            find = False
            for positon in rowscolsTable[modelIdx]:
                if MyUtils.filterStr(file, positon):
                    print("find: ", positon)
                    find = True
                    break
            if MyUtils.filterStr(file, "mapping_config"):
                find = True
           

            if not find:
                continue

            fromPath2 = fromPath+file
            toPath2 = toPath+file
            if copyImg:
                MyUtils.copyFile(fromPath2, toPath2, [ ".txt",".json", ".jpg", ".bmp", ".png"])
            else:
                MyUtils.copyFile(fromPath2, toPath2, [ ".txt",".json"])
            print("\n")

def copyModelFile(currentPath, dst_folders, files=[]):
    for file in files:
        source_file = currentPath+ '\\'+file
        toPath = dst_folders + "\\"+file
        fileFmt = os.path.splitext(source_file)[-1]
        res = MyUtils.copyFile(source_file, toPath, [fileFmt])

def modelCopy(modelPath, dst_folders,modelIdx, copyImgs=False):

    print("目标文件夹的路径: ", dst_folders)
    #删除mask文件夹
    toPath = dst_folders+"\\masks\\"
    MyUtils.deleteDir(toPath)
    MyUtils.createDirs(toPath)
    res = MyUtils.copyDir(modelPath+ '\\masks', toPath)
    
    # 要复制的文件
    source_file = modelPath+ '\\bin\\'
    toPath = dst_folders + "\\bin\\"
    MyUtils.createDirs(toPath)
    MyUtils.copyDir(source_file, toPath)

    source_file = modelPath+ '\\defectType\\'
    toPath = dst_folders + "\\defectType\\"
    MyUtils.createDirs(toPath)
    MyUtils.copyDir(source_file, toPath)

    source_file = modelPath+ '\\masks\\'
    toPath = dst_folders + "\\masks\\"
    MyUtils.createDirs(toPath)
    MyUtils.copyDir(source_file, toPath)

    source_file = modelPath+ '\\Parameters\\'
    toPath = dst_folders + "\\Parameters\\"
    MyUtils.createDirs(toPath)
    MyUtils.copyDir(source_file, toPath)
    files, wholeFiles = MyUtils.recursive_listdir(toPath)
    for file in files:
        jsonFile = toPath+file
        data = MyUtils.readJsonFile(jsonFile)
        if 'preLoad' in data:
            data['preLoad'] = True
        if 'preLoadImg' in data:
            data['preLoadImg'] = True
        if '_isDebug_' in data:
            data['_isDebug_'] = False
        if 'reWriteJson' in data:
            data['reWriteJson'] = False
        MyUtils.writeJsonFile(jsonFile, data)

    source_file = modelPath+ '\\ROI\\'
    toPath = dst_folders + "\\ROI\\"
    MyUtils.createDirs(toPath)
    MyUtils.copyDir(source_file, toPath)

    modelFiles = ['algDef.json','BasicSetting.xml', 'camDef.json', 
                  'DefectDefine.xml', 'Image.json', 'Layers.xml',
                  'model.gscf', 'Model.json', 'modules.json','ROI.xml',
                  'z-record.me','布局图.png'
                  ]
    copyModelFile(modelPath, dst_folders, modelFiles)

    ####################   模板文件
    #整个文件夹copy
    toDir = ""
    #匹配模板
    source_file = modelPath+ '\\resource\\templates\\1\\'
    toPath = dst_folders + '\\resource\\templates\\1\\'
    MyUtils.createDirs(toPath)
    MyUtils.copyDir(source_file, toPath)


    #油墨整图mask变换
    source_file = modelPath+ '\\resource\\masks\\mask-ink-warp\\'
    toDir = dst_folders +"\\resource\\masks\\mask-ink-warp\\"
    MyUtils.createDirs(toDir)
    MyUtils.copyDir(source_file, toDir)
    
    #根据位置映射关系选择性copy
    #油墨区
    copyModelFiles(modelPath, dst_folders,'\\resource\\masks\\mask-ink\\',modelIdx, copyImgs)
    #金手指区
    copyModelFiles(modelPath, dst_folders,'\\resource\\masks\\mask-jinshouzhi\\',modelIdx, copyImgs)
    #焊盘区,所有焊盘
    copyModelFiles(modelPath, dst_folders,'\\resource\\masks\\mask-pad-V\\',modelIdx, copyImgs)
    copyModelFiles(modelPath, dst_folders,'\\resource\\masks\\mask-pad-V2\\',modelIdx, copyImgs)
    copyModelFiles(modelPath, dst_folders,'\\resource\\masks\\mask-pad-H\\',modelIdx, copyImgs)
    copyModelFiles(modelPath, dst_folders,'\\resource\\masks\\mask-pad-H2\\',modelIdx, copyImgs)
    copyModelFiles(modelPath, dst_folders,'\\resource\\masks\\mask-pad-all\\',modelIdx, copyImgs)
    #焊盘区,大焊盘
    copyModelFiles(modelPath, dst_folders,'\\resource\\masks\\mask-pad-large\\',modelIdx, copyImgs)
    #焊盘区,选择的焊盘
    copyModelFiles(modelPath, dst_folders,'\\resource\\masks\\mask-pad-selected\\',modelIdx, copyImgs)
    copyModelFiles(modelPath, dst_folders,'\\resource\\masks\\mask-pad-selected2\\',modelIdx, copyImgs)
    #底图, 图
    copyModelFiles(modelPath, dst_folders,'\\resource\orgImage\\',modelIdx, copyImgs)

    return

if __name__ == "__main__":   
    models = [60,61,62,63,64,65,66,67,68,69]
    rowscolsTable = {60:["row1col2", "row1col4", "row1col6", "row1col8",
                         "row2col2", "row2col4", "row2col6", "row2col8",
                         "row3col2", "row3col4", "row3col6", "row3col8"
                         ],
                    61:["row1col1", "row1col3", "row1col7", "row1col9",
                         "row2col1", "row2col3", "row2col7", "row2col9",
                         "row3col1", "row3col3", "row3col7", "row3col9"
                         ],
                    62:["row4col2", "row4col4", "row4col6", "row4col8",
                         "row5col2", "row5col4", "row5col6", "row5col8",
                         "row6col2", "row6col4", "row6col6", "row6col8"
                         ],
                    63:["row4col1", "row4col3", "row4col7", "row4col9",
                         "row5col1", "row5col3", "row5col7", "row5col9",
                         "row6col1", "row6col3", "row6col7", "row6col9"
                         ],
                    64:["row7col2", "row7col4", "row7col6", "row7col8",
                         "row8col2", "row8col4", "row8col6", "row8col8",
                         "row9col2", "row9col4", "row9col6", "row9col8"
                         ],
                    65:["row7col1", "row7col3", "row7col7", "row7col9",
                         "row8col1", "row8col3", "row8col7", "row8col9",
                         "row9col1", "row9col3", "row9col7", "row9col9"
                         ],
                    66:["row10col2", "row10col4", "row10col6", "row10col8",
                         "row11col2", "row11col4", "row11col6", "row11col8",
                         "row12col2", "row12col4", "row12col6", "row12col8"
                         ],
                    67:["row10col1", "row10col3", "row10col7", "row10col9",
                         "row11col1", "row11col3", "row11col7", "row11col9",
                         "row12col1", "row12col3", "row12col7", "row12col9"
                         ],
                    68:["row1col5", "row1col10", "row2col5", "row2col10",
                         "row3col5", "row3col10", "row4col5", "row4col10",
                         "row5col5", "row5col10", "row6col5", "row6col10"
                         ],
                    69:["row7col5", "row7col10", "row8col5", "row8col10",
                         "row9col5", "row9col10", "row10col5", "row10col10",
                         "row11col5", "row11col10", "row12col5", "row12col10"
                         ]
                    }
    
    currentTime = datetime.datetime.now().strftime('%Y-%m-%d  %H:%M:%S')
    currentDate = datetime.datetime.now().strftime('%Y-%m-%d')
    
    #currentPath = os.getcwd()
    
    
    #写入当前修改时间
    isInit = 0    #是否初始建模
    idx_0 = 15.6
    modelPath = 'F:/workespace/woge-images/'+str(idx_0)+'_inch'
    modelName = "model-"+str(idx_0)+"inch-"

    commitInfo = modelName+"添加金手指密集小黑点检测"
    commit = commitInfo+currentDate
    toPath = modelPath+ "/"+modelName+"/z-record.me"
    print("调试记录的路径: ", toPath)

    if( not os.path.exists(modelPath)):
        print("path not exist: ", modelPath)
        exit(0)
    if isInit:
        with open(toPath,"w",  encoding='utf-8') as f:
            f.write(currentTime)
            f.write("->\n")
            f.write(commit)
            f.write("\n")
    else:
        with open(toPath,"a+",  encoding='utf-8') as f:
            f.write(currentTime)
            f.write("->\n")
            f.write(commit)
            f.write("\n")
    
    aimModelPath=[]
    #models = [60]
    
    copyImgs = True
    for i in models:
        toPath = modelName + str(i)
        aimModelPath.append(toPath)
        modelCopy(modelPath+"/"+modelName, modelPath+"/"+commit+"/"+ toPath,i, copyImgs)

    if 0:
        zipPath = modelPath+"/"+commit
        MyUtils.createDirs(zipPath)
        MyUtils.zipDir(zipPath, zipPath+".zip")
        MyUtils.deleteDir(zipPath)
    exit(0)
   
            

#http://10.127.0.188/, 软件授权网站
#xcopy CoreLibs\dll\*.dll   x64\Release\  /I /S /Y  